package com.yq.cp.controller;

import com.yq.cp.service.permission.UserService;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.permission.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    @Resource
    private UserService userService;

    @PostMapping("login")
    public ApiResult login(@RequestBody User user, HttpServletRequest request){
        log.info("登录>>"+user.getUsername());
        Map<String, Object> userinfo = userService.login(user);
        return ApiResult.ok().data(userinfo);
    }


    @PostMapping("getToken")
    public ApiResult getToken(@RequestBody User user){
        Map<String, Object> userinfo = userService.getToken(user);
        return ApiResult.ok().data(userinfo);
    }

    /**
     * 切换身份
     */
    @PostMapping("cut")
    public ApiResult cut() {
        Map<String, Object> cut = userService.cut();
        return ApiResult.ok().data(cut);
    }
    @PostMapping("getCode")
    public ApiResult getCode(){
        String code = userService.getCode();
        Map<String, Object> map=new HashMap<>();
        map.put("code",code);
        return ApiResult.ok().data(map);
    }
}